package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.Global;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.CachedCommandUtil;
import com.hzhr.iot.collect.util.DateUtil;
import com.hzhr.iot.core.util.HzhrStringUtils;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataModel;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataPostModel;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import com.hzhr.iot.device.sdk.service.Device_CollectDataService;
import com.hzhr.iot.device.sdk.service.Device_CollectDeviceService;
import com.hzhr.iot.device.sdk.service.Device_IotDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * NBIOT 物联网表日用量历史查询 处理器
 * @author 48310
 *
 */
@Component
public class MessageProcessor14 implements MessageProcessor {
	@Autowired
	Device_CollectDataService device_collectDataService;
	@Autowired
	Device_IotDeviceService device_iotDeviceService;
	@Autowired
	Device_CollectDeviceService device_collectDeviceService;
	@Autowired
	Global global;
	@Autowired
	CachedCommandUtil cachedCommandUtil;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		//查询物联网设备
		IotDeviceModel iotDeviceModel = null;
		String iotDeviceCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID));
		if (!StringUtils.isEmpty(iotDeviceCode)) {
			Map<String, String> header = new HashMap<>();
			header.put("token", global.getFirstToken());
			Map<String, Object> query = new HashMap<>();
			query.put("_show", "one");
			query.put("_equal_iotPlatformDeviceId", deviceId);
			iotDeviceModel = device_iotDeviceService.get(header, query).getModel();
		}
		if (iotDeviceModel == null) throw new Exception("物联网设备不存在");

		Integer iotDeviceId = iotDeviceModel.getId();

		//查询采集设备
		Map<String, String> header = new HashMap<>();
		header.put("token", global.getFirstToken());
		Map<String, Object> query = new HashMap<>();
		query.put("_show", "one");
		query.put("_equal_iotDeviceId", iotDeviceModel.getId());
		CollectDeviceModel collectDeviceModel = device_collectDeviceService.get(header, query).getModel();

		Integer collectDeviceId = null;
		if (collectDeviceModel != null) {
			collectDeviceId = collectDeviceModel.getId();
		}

		@SuppressWarnings("unchecked")
		List<String> currentDateStrList = (List<String>)parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_DATE);
		@SuppressWarnings("unchecked")
		List<String> currentTimeStrList = (List<String>)parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_TIME);
		@SuppressWarnings("unchecked")
		List<String> currentValueStrList = (List<String>)parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_VALUE);

		for (int i = 0; i < currentDateStrList.size(); i++) {
			String collectingDateStr = currentDateStrList.get(i) + currentTimeStrList.get(i);
			Date collectingDate = DateUtil.strToDate(collectingDateStr, "yyyyMMddHHmmss");
			Double currentValue =  Double.parseDouble(currentValueStrList.get(i));

			Map<String, String> header1 = new HashMap<>();
			header.put("token", global.getFirstToken());
			Map<String, Object> query1 = new HashMap<>();
			query.put("_show", "one");
			query.put("_equal_iotDeviceId", deviceId);
			query.put("_equal_currentTm", collectingDate.getTime());
			CollectDataModel existCollectData = device_collectDataService.get(header1, query1).getModel();
			if (existCollectData == null) {
				CollectDataPostModel collectDataPostModel = new CollectDataPostModel();
				collectDataPostModel.setIotDeviceId(iotDeviceId);
				collectDataPostModel.setCollectDeviceId(collectDeviceId);
				collectDataPostModel.setCurrentTm(collectingDate.getTime());
				collectDataPostModel.setCurrentValue(currentValue);
				device_collectDataService.post(header1, new HashMap<>(), collectDataPostModel);
			}
		}

		String lastFrameFlag = HzhrStringUtils.obj2str(parsedMessageObj.get(MessageFieldKeyEnum.LAST_FRAME_FLAG));
		String messageId = HzhrStringUtils.obj2str(parsedMessageObj.get(MessageFieldKeyEnum.MESSAGE_ID));
		if ("1".equals(lastFrameFlag)) {//如果是最后一帧，则清除缓存
			cachedCommandUtil.deleteCachedCommand(deviceId, iotDeviceCode, messageId);
		}
		return null;
	}
}
